package com.tomcat;

import com.thetransactioncompany.cors.CORSFilter;
import example.App;
import org.apache.catalina.Context;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class InsideTomcat {
    // tomcat的端口号
    private static String port;
    // tomcat的字符编码集
    private static String code;
    // 拦截请求路径
    private static String hinderURL;
    // 请求转发路径
    private static String shiftURL;
    // tomcat对象
    private static Tomcat tomcat;
    // servlet对象
    private static HttpServlet servlet;

    InsideTomcat() {
        try {
            init();
        } catch (LifecycleException | ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 启动这个内嵌tomcat容器
    public static void run() throws LifecycleException {
        InsideTomcat insideTomcat = new InsideTomcat();
        // 启动tomcat
        insideTomcat.tomcat.start();
        // 保持tomcat的启动状态
        insideTomcat.tomcat.getServer().await();
    }

    // 初始化tomcat容器
    public void init() throws LifecycleException, ServletException {
        // 执行init方法时加载默认属性值
        // 为了演示这里写了死值进去
        port = "8080";
        code = "UTF-8";
        hinderURL = "/";
        shiftURL = "/";
        tomcat = new Tomcat();
        // HttpServlet对象是你现有的自定义的Servlet容器
        App app = new App();
        app.initialize(new FcContext());
        servlet = new FcServlet(app);

        // 创建连接器
        Connector conn = tomcat.getConnector();
        conn.setPort(Integer.valueOf(port));
        conn.setURIEncoding(code);

        // 设置Host
        Host host = tomcat.getHost();
        host.setAppBase("webapps");

        // 获取目录绝对路径
        String classPath = System.getProperty("user.dir");

        // 配置tomcat上下文
        Context context = tomcat.addContext(host, hinderURL, classPath);
        FilterDef filterDef = new FilterDef();
        filterDef.setFilterName("CORS");
        filterDef.setFilter(new CORSFilter());
        context.addFilterDef(filterDef);

        FilterMap filterMap = new FilterMap();
        filterMap.addURLPattern("/*");
        filterMap.setFilterName("CORS");
        context.addFilterMap(filterMap);

        // 添加Servlet
        Wrapper wrapper = Tomcat.addServlet(context, "servlet", servlet);

        wrapper.addMapping(shiftURL);

    }

    /*
     * 以下Setters和Getters方法提供外部访问或灵活设置
     */
    public static String getPort() {
        return port;
    }
    public static void setPort(String port) {
        InsideTomcat.port = port;
    }
    public static String getCode() {
        return code;
    }
    public static void setCode(String code) {
        InsideTomcat.code = code;
    }
    public static String getHinderURL() {
        return hinderURL;
    }
    public static void setHinderURL(String hinderURL) {
        InsideTomcat.hinderURL = hinderURL;
    }
    public static String getShiftURL() {
        return shiftURL;
    }
    public static void setShiftURL(String shiftURL) {
        InsideTomcat.shiftURL = shiftURL;
    }
    public static Tomcat getTomcat() {
        return tomcat;
    }
    public static void setTomcat(Tomcat tomcat) {
        InsideTomcat.tomcat = tomcat;
    }
    public static HttpServlet getServlet() {
        return servlet;
    }
    public static void setServlet(HttpServlet servlet) {
        InsideTomcat.servlet = servlet;
    }
}
